<template>
  <el-dialog
    title="编辑标签"
    :visible.sync="dialogVisible"
    @close="dialogFormVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form size="small" :model="form" :rules="formRules" ref="form">
      <!-- <el-form-item
        label="绑定地区"
        :label-width="formLabelWidth"
        prop="region_id"
      >
        <el-cascader
          v-model="form.region_id"
          :options="regions"
          :props="{ value: 'id', label: 'name' }"
          clearable
        >
        </el-cascader>
      </el-form-item> -->
      <el-form-item
        label="名称"
        :label-width="formLabelWidth"
        prop="label_name"
      >
        <el-input v-model="form.label_name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="描述" :label-width="formLabelWidth">
        <el-input v-model="form.info" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="状态" :label-width="formLabelWidth">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">显示</el-radio>
          <el-radio :label="0">隐藏</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序号" :label-width="formLabelWidth">
        <el-input v-model="form.sort" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible">取 消</el-button>
      <el-button type="primary" @click="editCategory" :loading="loading"
        >确 定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import LabelApi from "@/api/productLabel.js";
import IndexApi from "@/api/index.js";
export default {
  components: {},
  data() {
    return {
      form: {
        product_label_id: 0,
        label_name: "",
        info: "",
        status: 1,
        sort: 0,
        region_id: 0
      },
      formRules: {
        region_id: [{ required: true, message: "请选择区域", trigger: "blur" }],
        label_name: [{ required: true, message: "请输入名称", trigger: "blur" }]
      },
      /*左边长度*/
      formLabelWidth: "120px",
      /*是否显示*/
      dialogVisible: false,
      loading: false,
      regions: []
    };
  },
  props: ["open_edit", "editform"],
  created() {
    this.getList();
    this.dialogVisible = this.open_edit;
    this.form.region_id = [
      this.editform.province_id,
      this.editform.city_id,
      this.editform.county_id
    ];
    this.form.product_label_id = this.editform.product_label_id;
    this.form.label_name = this.editform.label_name;
    this.form.info = this.editform.info;
    this.form.status = this.editform.status;
    this.form.sort = this.editform.sort;
  },
  methods: {
    getList() {
      let self = this;
      IndexApi.getProxyRegionList({}, true).then(res => {
        self.regions = [];
        Object.keys(res.data.regions).forEach(key => {
          self.regions.push(res.data.regions[key]);
        });
      });
    },
    /*修改*/
    editCategory() {
      let self = this;
      let params = this.form;
      self.$refs.form.validate(valid => {
        if (valid) {
          self.loading = true;
          LabelApi.editLabel(params, true)
            .then(data => {
              self.loading = false;
              self.$message({
                message: "恭喜你，修改成功",
                type: "success"
              });
              self.dialogFormVisible(true);
            })
            .catch(error => {
              self.loading = false;
            });
        }
      });
    },

    /*关闭弹窗*/
    dialogFormVisible(e) {
      if (e) {
        this.$emit("closeDialog", {
          type: "success",
          openDialog: false
        });
      } else {
        this.$emit("closeDialog", {
          type: "error",
          openDialog: false
        });
      }
    }
  }
};
</script>

<style></style>
